SPB Git

Repository Public

route.ts 4.1 KB 73 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : src/app/api/auth/passkeys/[id]/route.ts9 * Module           : app10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1713 * Last Modified    : 2026-11-1714 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { z } from "zod";44import { and, count, eq, sql as dsql } from "drizzle-orm";45import { db, schema } from "@/db";46import { handler, parseBody, HttpError } from "@/lib/api";47import { revokeSessionsOfCredential } from "@/lib/auth/sessions";48import { audit } from "@/lib/audit";49const idOf = (p: Record<string, string>) => { const id = decodeURIComponent(p.id ?? ""); if (!/^[A-Za-z0-9_-]{8,1024}$/.test(id)) throw new HttpError(400, "Bad passkey id"); return id; };50/** PATCH { label } → rename (fresh verification required). */51export const PATCH = handler(async ({ req, user, params }) => {52  const id = idOf(params); const b = await parseBody(req, z.object({ label: z.string().trim().min(1).max(60) }));53  const r = await db.update(schema.webauthnCredentials).set({ label: b.label }).where(and(eq(schema.webauthnCredentials.id, id), eq(schema.webauthnCredentials.userId, user.id))).returning({ id: schema.webauthnCredentials.id });54  if (!r.length) throw new HttpError(404, "Passkey not found");55  await audit(user.id, "auth.passkey_renamed", { type: "passkey", id: id.slice(0, 16) }, { label: b.label });56  return Response.json({ ok: true });57}, { fresh: true });58/** DELETE → revoke a passkey and every session it opened. The last passkey can never be removed (there is no other way in). */59export const DELETE = handler(async ({ user, params }) => {60  const id = idOf(params);61  const removed = await db.transaction(async (tx) => {62    // Serialise concurrent revocations so two requests cannot each delete "the other" last passkey.63    await tx.execute(dsql`select pg_advisory_xact_lock(761001)`);64    const [{ n }] = await tx.select({ n: count() }).from(schema.webauthnCredentials).where(eq(schema.webauthnCredentials.userId, user.id));65    if (n <= 1) throw new HttpError(409, "This is your only passkey. Add another one before removing it.", "last_passkey");66    return tx.delete(schema.webauthnCredentials).where(and(eq(schema.webauthnCredentials.id, id), eq(schema.webauthnCredentials.userId, user.id))).returning({ label: schema.webauthnCredentials.label });67  });68  if (!removed.length) throw new HttpError(404, "Passkey not found");69  const sessions = await revokeSessionsOfCredential(id, "passkey-revoked");70  await audit(user.id, "auth.passkey_revoked", { type: "passkey", id: id.slice(0, 16) }, { label: removed[0].label, sessionsRevoked: sessions });71  return Response.json({ ok: true, sessionsRevoked: sessions, signedOut: id === user.credentialId });72}, { fresh: true });73
Files main
Menu

Theme